package com.kinglone.util;

import java.math.BigDecimal;
import org.apache.log4j.Logger;

public class ValueUtil
{
  private static final Logger log = Logger.getLogger(ValueUtil.class);

  public static String getString(Object value)
  {
    String result = "";
    if (!(ObjectUtil.isEmpty(value))) {
      String sValue = value.toString().trim();
      if (value instanceof Number) {
        if ((value instanceof Double) || (value instanceof BigDecimal)) {
          if ((!("Infinity".equals(sValue))) && (!("NaN".equals(sValue))))
            result = StringUtil.toNuSicen(value);
          else
            result = "0";
        }
        else
          result = sValue;
      }
      else {
        result = sValue;
      }
    }
    return result.trim();
  }

  public static long getLong(Object value)
  {
    try
    {
      return Long.parseLong(getString(value)); } catch (Exception e) {
    }
    return 0L;
  }

  public static double getDouble(Object value)
  {
    try
    {
      return Double.parseDouble(getString(value)); } catch (Exception e) {
    }
    return 0.0D;
  }

  public static int getInt(Object value)
  {
    try
    {
      return Integer.parseInt(getString(value)); } catch (Exception e) {
    }
    return 0;
  }

  public static boolean getBoolean(Object value)
  {
    try
    {
      String v = getString(value);
      if ("1".equals(v))
        return true;
      if ("0".equals(v))
        return false;
      if ("Y".equals(v))
        return true;
      if ("N".equals(v)) {
        return false;
      }
      return Boolean.parseBoolean(v);
    } catch (Exception e) {
    }
    return false;
  }

  public static boolean isNullOrZero(Integer value)
  {
    try
    {
      String v = getString(value);
      if ("0".equals(v)) {
        return true;
      }
      return (!("null".equals(v)));
    }
    catch (Exception e)
    {
    }

    return false;
  }
}